<?php
namespace app\home\controller;
use \think\Validate;
use \think\captcha\Captcha;
use think\Session;

class Index extends \think\Controller
{
    public function index()
    {
        $type_list = db("food_type")->select();
        $hot_info = db("food")->where("type='热销'")->select();
        $hot_info = array_chunk($hot_info,2);
    	$user_id = session("user_id");
        $this->assign("typeaa","picker");
        $this->assign("hot_info",$hot_info);
        $this->assign("type_list",$type_list);
        $this->assign("user_id",$user_id);
    	return view('index');
    }

    public function getFoodList(){
        if(input('post.type')!=null){
            $type = input('post.type');
            $type_list = db("food")->where("type='{$type}'")->select();
        }else{
            $type_list = db("food")->where("type='饮料'")->select();
        }
        return $type_list;
    }
    
    public function search(){
    	$user_id = session("user_id");
        $condition = input('post.condition');   //搜索条件 
        $where['title'] = ['like', "%$condition%"];
        $whereOr['content'] = ['like', "%$condition%"];
        $snew_list  = db('new')->alias('n')->join("admin a","n.admin_id=a.admin_id")->field("n.*,a.admin_name")->where($where)->whereOr($whereOr)->select();
        $note_list = db('note')->alias('n')->join("user u","n.user_id=u.user_id")->field("n.*,u.user_name")->where("note_status = 1")->where($where)->whereOr($whereOr)->select();
        if(!empty($user_id)){
    		$result  = db("user")->where("user_id = {$user_id}")->find();
    	}
    	else{
    		$user_id="";
    		$result=[];
            $result['user_name']="普通用户";
        }

        $hot_list = db("note")->where("note_status = 1")->limit(5)->order('note_visit desc')->select();      //最热帖子列表
        $lastest_list = db("note")->where("note_status = 1")->order("addtime desc")->limit(6)->select();  //最新帖子列表
        $new_list = db("new")->order("addtime desc")->limit(5)->select();       //新闻列表
        $this->assign("hot_list",$hot_list);
        $this->assign("new_list",$new_list);
        $this->assign("lastest_list",$lastest_list);
        $this->assign("result",$result);
        $this->assign("snew_list",$snew_list);
        $this->assign("note_list",$note_list);
    	return view('search');
    }

    public function logout(){
        Session::clear();
        $this->redirect('home/login/login');
    }

    public function checkLogin(){
        if( session('user_id') == NULL )
		{
			$this->error("请登录",'home/login/login');
		}
    }
}
